<?php


namespace app\api\controller;


use app\common\controller\Api;

class Pay extends Api
{
    protected $noNeedLogin = ['notify','handWxMiniNotify','pcPayQuery'];
    protected $noNeedRight = ['*'];


    /**
     * 支付
     *
     * @param string $platform 平台:pc=PC,mini=微信小程序,app=App
     * @param string $pay_type 支付类型:alipay=支付宝,wxpay=微信,wallet=钱包
     * @param string $order_no 订单号
     */
    public function pay(\app\common\model\order\PayModel $PayModel, \app\api\validate\PayValidate $PayValidate)
    {
        $params = $this->request->post();
        if(!$PayValidate->check($params,[],'pay')){
            $this->errorReturn(40001,$params,$PayValidate->getError());
        }
        $params['openid'] = $this->auth->openid;
        $user_id = $this->auth->id;
        $return = $PayModel->pay($params, $user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 支付下单回调处理
     */
    public function notify(\app\common\model\order\PayModel $PayModel)
    {
        $paytype = $this->request->param('paytype');
        $PayModel->notify($paytype);
    }

    /**
     * PC扫码支付结果查询
     * @param string $paytype 支付类型:alipay=支付宝,wechat=微信
     * @param string $out_trade_no 订单号
     */
    public function pcPayQuery(\app\common\model\order\PayModel $PayModel, \app\api\validate\PayValidate $PayValidate)
    {
        $params = $this->request->post();
        if(!$PayValidate->check($params,[],'pcPayQuery')){
            $this->errorReturn(40001,$params,$PayValidate->getError());
        }
        $return = $PayModel->pcPayQuery($params);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 微信小程序支付下单回调处理(手动补发)
     */
    public function handWxMiniNotify(\app\common\model\order\PayModel $PayModel)
    {
        $out_trade_no = $this->request->request('out_trade_no');
        $time_end = $this->request->request('time_end');
        $transaction_id = $this->request->request('transaction_id');
        $total_fee = $this->request->request('total_fee');
        $params = [
            'out_trade_no' => $out_trade_no,
            'time_end' => $time_end,
            'transaction_id' => $transaction_id,
            'total_fee' => $total_fee,
        ];
        $res = $PayModel->wxMiniNotify($params);
        if(!$res){
            $this->error('失败');
        }
        $this->success('OK');
    }



}